<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.example.demo.grbkxt.model.User" %>
<!DOCTYPE html>
<html>
<head>
    <title>用户管理</title>
    <%@ include file="/WEB-INF/views/common/header.jsp" %>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>用户管理</h1>
            <div class="nav-links">
                <a href="${pageContext.request.contextPath}/blog/admin" class="btn">博客管理</a>
                <a href="${pageContext.request.contextPath}/admin/settings" class="btn">系统设置</a>
                <a href="${pageContext.request.contextPath}/logout" class="btn btn-danger">退出</a>
            </div>
        </div>
        
        <div class="user-form">
            <h2>添加新用户</h2>
            <form action="${pageContext.request.contextPath}/admin/users" method="post">
                <div class="form-group">
                    <label>用户名：</label>
                    <input type="text" name="username" required>
                </div>
                <div class="form-group">
                    <label>密码：</label>
                    <input type="password" name="password" required>
                </div>
                <div class="form-group">
                    <label>
                        <input type="checkbox" name="isAdmin"> 管理员权限
                    </label>
                </div>
                <button type="submit" class="btn btn-primary">添加用户</button>
            </form>
        </div>

        <div class="user-list">
            <h2>用户列表</h2>
            <table>
                <thead>
                    <tr>
                        <th>用户名</th>
                        <th>角色</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <% 
                    List<User> users = (List<User>)request.getAttribute("users");
                    if(users != null) {
                        for(User user : users) { 
                    %>
                    <tr>
                        <td><%= user.getUsername() %></td>
                        <td><%= user.isAdmin() ? "管理员" : "普通用户" %></td>
                        <td>
                            <a href="${pageContext.request.contextPath}/admin/users/edit?id=<%= user.getId() %>" 
                               class="btn btn-primary">编辑</a>
                            <button onclick="deleteUser('<%= user.getId() %>')" 
                                    class="btn btn-danger">删除</button>
                        </td>
                    </tr>
                    <% 
                        }
                    } 
                    %>
                </tbody>
            </table>
        </div>
    </div>
    <script>
        function deleteUser(username) {
            if(confirm('确定要删除用户 ' + username + ' 吗？')) {
                fetch('${pageContext.request.contextPath}/admin/users?username=' + username, {
                    method: 'DELETE'
                }).then(() => window.location.reload());
            }
        }
    </script>
</body>
</html>
